cmake_minimum_required (VERSION 2.8.9)

PROJECT (cloudy-storage-lib)

IF(CMAKE_SYSTEM_NAME STREQUAL "Linux" OR CMAKE_SYSTEM_NAME STREQUAL "FreeBSD")
    SET (CMAKE_INSTALL_PREFIX  "/opt/has")
ENDIF()
IF(CMAKE_SYSTEM_NAME STREQUAL "Windows")
    SET (CMAKE_INSTALL_PREFIX  "/")
ENDIF()

SET (PROJECT_MODULE_EPOCH_NUMBER  0)
SET (PROJECT_MODULE_VERSION_MAJOR 1)
SET (PROJECT_MODULE_VERSION_MINOR 0)
SET (PROJECT_MODULE_VERSION_BUILD 0)
SET (PROJECT_MODULE_VERSION_PATCH ${PROJECT_MODULE_VERSION_BUILD})
SET (PROJECT_MODULE_VERSION_REVISION 0)

SET (PROJECT_MODULE_LIBRARY_SOURCES src/FileThread.cpp src/AgentProfile.cpp src/Application.cpp src/Cloudy.cpp src/File.cpp src/Storage.cpp src/Validator.cpp src/IConnector.cpp src/TorrentConnector.cpp src/Channel.cpp)
SET (PROJECT_MODULE_TEST_SOURCES src/CppUnitRunner.cpp src/ApplicationTest.cpp src/AgentProfileTest.cpp src/StorageTest.cpp src/ValidatorTest.cpp)

SET (EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)
SET (BINARY_OUTPUT_PATH     ${PROJECT_BINARY_DIR}/bin)
SET (LIBRARY_OUTPUT_PATH    ${PROJECT_BINARY_DIR}/lib)
SET (INCLUDE_OUTPUT_PATH    ${PROJECT_BINARY_DIR}/include)
SET (MAN_OUTPUT_PATH        ${PROJECT_BINARY_DIR}/man)
SET (DOC_OUTPUT_PATH        ${PROJECT_BINARY_DIR}/doc)

SET (CMAKE_CONFIGURATION_TYPES "Release;RelWithDebInfo;Debug" CACHE STRING "Configurations" FORCE)

SET(CMAKE_ALLOW_LOOSE_LOOP_CONSTRUCTS true)

IF(CMAKE_SYSTEM_NAME STREQUAL "Windows" )
    SET (CLOUDY_LINKING          "cloudyshared.lib")
    SET (CLOUDY_TEST_LINKING     "cloudyshared.lib")
    # Include dirs on windows have linking problems - spaces makes from path separate commandline arguments
    SET (BOOST_INCLUDE_DIR        "C:/Program Files/boost_1_51_0")
    SET (JDK_INCLUDE_DIR          "C:/Program Files/Java/jdk1.6.0_37/include")
    SET (JDK_INCLUDE_OS_DIR       "C:/Program Files/Java/jdk1.6.0_37/include/win32")
    SET (JDK_LIB_DIR              "C:/Program Files/Java/jdk1.6.0_37/lib")
    SET (BOOST_LIB_DIR            "C:/Program Files/boost_1_51_0/stage/lib")
    SET (CMAKE_CXX_FLAGS_DEBUG    "${CMAKE_CXX_FLAGS_DEBUG} /D CLOUDY_EXPORTS")
    SET (CMAKE_CXX_FLAGS_RELEASE  "${CMAKE_CXX_FLAGS_RELEASE} /D CLOUDY_EXPORTS")
    INCLUDE_DIRECTORIES(${BOOST_INCLUDE_DIR})
    INCLUDE_DIRECTORIES("${PROJECT_BINARY_DIR}/include")
    INCLUDE_DIRECTORIES(${JDK_INCLUDE_DIR})
    INCLUDE_DIRECTORIES(${JDK_INCLUDE_OS_DIR})
    LINK_DIRECTORIES("${BOOST_LIB_DIR}")
    LINK_DIRECTORIES(${JDK_LIB_DIR})
    LINK_DIRECTORIES("${PROJECT_BINARY_DIR}/lib")
ENDIF()

IF(CMAKE_SYSTEM_NAME STREQUAL "FreeBSD")
    IF(CMAKE_BUILD_TYPE STREQUAL "Debug")
        SET (CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g")
    ENDIF()
    INCLUDE_DIRECTORIES("/usr/local/include")
    INCLUDE_DIRECTORIES("/usr/local/openjdk7/include")
    INCLUDE_DIRECTORIES("/usr/local/openjdk7/include/freebsd")
    INCLUDE_DIRECTORIES("${PROJECT_BINARY_DIR}/include")
    SET (CLOUDY_LINKING      "-pthread -lboost_filesystem")
    SET (CLOUDY_TEST_LINKING "-pthread -lhasiddb")
ENDIF()

IF(CMAKE_SYSTEM_NAME STREQUAL "Linux" )
    #SET (CMAKE_CXX_FLAGS "-s")
    SET (CMAKE_BUILD_TYPE debug)
    SET (CMAKE_CXX_FLAGS_DEBUG "-g")
    INCLUDE_DIRECTORIES("${PROJECT_BINARY_DIR}/include")
    INCLUDE_DIRECTORIES("/usr/local/include")
    INCLUDE_DIRECTORIES("/usr/lib/jvm/java-openjdk/include/linux")
    INCLUDE_DIRECTORIES("/usr/lib/jvm/java-openjdk/include")
    SET (CLOUDY_LINKING      "-lcloudyshared -pthread -ltorrent -lboost_date_time-mt -lboost_serialization-mt -lboost_filesystem-mt -lboost_signals-mt -lboost_graph-mt -lboost_iostreams-mt -lboost_prg_exec_monitor-mt -lboost_program_options-mt -lboost_wave-mt -lboost_python-mt -lboost_wserialization-mt -lboost_regex-mt -ldl")
    SET (CLOUDY_TEST_LINKING "-lcloudyshared -pthread -ltorrent -lboost_date_time-mt -lboost_serialization-mt -lboost_filesystem-mt -lboost_signals-mt -lboost_graph-mt -lboost_iostreams-mt -lboost_prg_exec_monitor-mt -lboost_program_options-mt -lboost_wave-mt -lboost_python-mt -lboost_wserialization-mt -lboost_regex-mt -lboost_unit_test_framework-mt -ldl")
    SET (JDK_LIB_DIR "/usr/lib/jvm/java-openjdk/lib")
    SET (JDK_INCLUDE_DIR          "/usr/lib/jvm/java-openjdk/include")
    SET (JDK_INCLUDE_OS_DIR       "/usr/lib/jvm/java-openjdk/include/linux")
    SET (JDK_LIB_DIR              "/usr/lib/jvm/java-openjdk/lib")
    INCLUDE_DIRECTORIES("${PROJECT_BINARY_DIR}/include")
    INCLUDE_DIRECTORIES(${JDK_INCLUDE_DIR})
    INCLUDE_DIRECTORIES(${JDK_INCLUDE_OS_DIR})
    LINK_DIRECTORIES(${JDK_LIB_DIR})
    LINK_DIRECTORIES("${PROJECT_BINARY_DIR}/lib")
ENDIF()

IF(CMAKE_CONFIGURATION_TYPES)
   SET(CMAKE_CONFIGURATION_TYPES Debug Release)
   SET(CMAKE_CONFIGURATION_TYPES "${CMAKE_CONFIGURATION_TYPES}" CACHE STRING "Reset the configurations" FORCE)
ENDIF()

configure_file (
		"${PROJECT_SOURCE_DIR}/include/Version.h.in"
		"${PROJECT_BINARY_DIR}/include/Version.h"
	)
IF(CMAKE_SYSTEM_NAME STREQUAL "Linux" OR CMAKE_SYSTEM_NAME STREQUAL "FreeBSD")
    configure_file (
            "${PROJECT_SOURCE_DIR}/src/cloudy"
            "${PROJECT_BINARY_DIR}/bin/cloudy"
        )

    configure_file (
            "${PROJECT_SOURCE_DIR}/src/cloudy-devel"
            "${PROJECT_BINARY_DIR}/bin/cloudy-devel"
        )

    configure_file (
            "${PROJECT_SOURCE_DIR}/src/cloudy-test"
            "${PROJECT_BINARY_DIR}/bin/cloudy-test"
        )

    configure_file (
            "${PROJECT_SOURCE_DIR}/src/valgrind-test"
            "${PROJECT_BINARY_DIR}/bin/valgrind-test"
        )

    configure_file (
            "${PROJECT_SOURCE_DIR}/src/debug"
            "${PROJECT_BINARY_DIR}/bin/debug"
        )

    configure_file (
            "${PROJECT_SOURCE_DIR}/src/profile.d.cloudy.sh"
            "${PROJECT_BINARY_DIR}/etc/profile.d/cloudy.sh"
        )
ENDIF()

IF(CMAKE_SYSTEM_NAME STREQUAL "Linux" OR CMAKE_SYSTEM_NAME STREQUAL "FreeBSD")
    ADD_CUSTOM_TARGET(mainresources cp ./lib/libcloudy.so ../cmake-java-lib/src/main/resources)
    ADD_CUSTOM_TARGET(testresources cp ./lib/libcloudy.so ../cmake-java-lib/src/test/resources)
    ADD_CUSTOM_TARGET(resources   cp ./lib/libcloudy.so ../cmake-java-lib/src/main/resources && cp ./lib/libcloudy.so ../cmake-java-lib/src/test/resources)
    ADD_CUSTOM_TARGET(dirs        mkdir -p Build/Debug && mkdir -p Build/Release WORKING_DIRECTORY ./)
    ADD_CUSTOM_TARGET(analysis    cccc ./src/*.cpp ./include/*.h WORKING_DIRECTORY ./)
    ADD_CUSTOM_TARGET(cppcheck    cppcheck -v --xml --xml-version=2 --enable=all -I ${PROJECT_SOURCE_DIR}/include ${PROJECT_SOURCE_DIR}/src 2> check.xml WORKING_DIRECTORY ./)
    ADD_CUSTOM_TARGET(testing     ./cloudy-test DEPENDS test.bin WORKING_DIRECTORY ${PROJECT_BINARY_DIR}/bin)
    ADD_CUSTOM_TARGET(debuging    ./debug DEPENDS test.bin WORKING_DIRECTORY ${PROJECT_BINARY_DIR}/bin)
    ADD_CUSTOM_TARGET(valgrind    ./valgrind-test DEPENDS test.bin WORKING_DIRECTORY ${PROJECT_BINARY_DIR}/bin)
    ADD_CUSTOM_TARGET(doxygen     doxygen)
    ADD_CUSTOM_TARGET(gzipman     mkdir -p ${MAN_OUTPUT_PATH}/man1 && cp ${MAN_OUTPUT_PATH}/*.1 ${MAN_OUTPUT_PATH}/man1 && gzip -f ${MAN_OUTPUT_PATH}/man1/cloudy.1)
    ADD_CUSTOM_TARGET(binClean    rm -r ./bin)
    ADD_CUSTOM_TARGET(libClean    rm -r ./lib)
ENDIF()
IF(CMAKE_SYSTEM_NAME STREQUAL "Windows")
    ADD_CUSTOM_TARGET(dllCopy     cp lib/cloudyshared.dll bin/cloudyshared.dll)
ENDIF()

add_executable(cloudy.bin      src/main.cpp)
add_executable(test.bin         ${PROJECT_MODULE_TEST_SOURCES})
add_library(cloudystatic       STATIC ${PROJECT_MODULE_LIBRARY_SOURCES})
add_library(cloudyshared       SHARED ${PROJECT_MODULE_LIBRARY_SOURCES})

IF(CMAKE_SYSTEM_NAME STREQUAL "Linux" OR CMAKE_SYSTEM_NAME STREQUAL "FreeBSD")
    ADD_DEPENDENCIES(cloudy.bin     cloudyshared)
    ADD_DEPENDENCIES(cloudy.bin     cloudystatic)
    ADD_DEPENDENCIES(test.bin       cloudyshared)
    ADD_DEPENDENCIES(test.bin       cloudystatic)
    ADD_DEPENDENCIES(cloudy.bin     gzipman)
    ADD_DEPENDENCIES(mainresources  cloudyshared)
    ADD_DEPENDENCIES(testresources  cloudyshared)
    ADD_DEPENDENCIES(resources      mainresources)
    ADD_DEPENDENCIES(resources      testresources)
ENDIF()

IF(CMAKE_SYSTEM_NAME STREQUAL "Windows")
    ADD_DEPENDENCIES(cloudy.bin    dllCopy)
    ADD_DEPENDENCIES(dllCopy        cloudyshared)
    ADD_DEPENDENCIES(dllCopy        cloudystatic)
ENDIF()

TARGET_LINK_LIBRARIES(cloudy.bin "${CLOUDY_LINKING}")
TARGET_LINK_LIBRARIES(test.bin    "${CLOUDY_TEST_LINKING}")

IF(CMAKE_SYSTEM_NAME STREQUAL "Linux" OR CMAKE_SYSTEM_NAME STREQUAL "FreeBSD")
    INSTALL(TARGETS cloudyshared                         DESTINATION lib)
    INSTALL(TARGETS cloudystatic                         DESTINATION lib)
    INSTALL(TARGETS cloudy.bin                           DESTINATION bin)
    INSTALL(PROGRAMS ${EXECUTABLE_OUTPUT_PATH}/cloudy    DESTINATION bin)
    INSTALL(FILES "include/Development.h"                DESTINATION include)
    INSTALL(FILES "${MAN_OUTPUT_PATH}/man1/cloudy.1.gz" DESTINATION man/man1)
    INSTALL(FILES "etc/profile.d/cloudy.sh"             DESTINATION etc/profile.d)
    INSTALL(FILES "doc/cloudy.txt"                      DESTINATION doc)
ENDIF()
IF(CMAKE_SYSTEM_NAME STREQUAL "Windows")
    INSTALL(TARGETS cloudyshared                        DESTINATION bin)
    INSTALL(TARGETS cloudystatic                        DESTINATION bin)
    INSTALL(TARGETS cloudy.bin                          DESTINATION bin)
ENDIF()

ENABLE_TESTING()
IF(CMAKE_SYSTEM_NAME STREQUAL "Linux" OR CMAKE_SYSTEM_NAME STREQUAL "FreeBSD")
    ADD_TEST(FuncoTest               ${EXECUTABLE_OUTPUT_PATH}/cloudy-test  --run_test=FuncoTest)
    ADD_TEST(ApplicationTest         ${EXECUTABLE_OUTPUT_PATH}/cloudy-test  --run_test=ApplicationTest)
    ADD_TEST(ValgrindFuncoTest       ${EXECUTABLE_OUTPUT_PATH}/valgrind-test --run_test=FuncoTest)
    ADD_TEST(ValgrindApplicationTest ${EXECUTABLE_OUTPUT_PATH}/valgrind-test --run_test=ApplicationTest)
ENDIF()
IF(CMAKE_SYSTEM_NAME STREQUAL "Windows")
    ADD_TEST(FuncoTest               ${EXECUTABLE_OUTPUT_PATH}/test.bin  --run_test=FuncoTest)
    ADD_TEST(ApplicationTest         ${EXECUTABLE_OUTPUT_PATH}/test.bin  --run_test=ApplicationTest)
ENDIF()

IF(CMAKE_SYSTEM_NAME STREQUAL "Linux")
    SET(CPACK_RPM_PACKAGE_DEBUG               true)
    SET(CPACK_RPM_POST_INSTALL_SCRIPT_FILE    "${PROJECT_SOURCE_DIR}/src/post.install.sh")
    SET(CPACK_RPM_POST_UNINSTALL_SCRIPT_FILE  "${PROJECT_SOURCE_DIR}/src/un.install.sh")
    SET(CPACK_RPM_PACKAGE_SUMMARY             "Summary")
    SET(CPACK_RPM_PACKAGE_NAME                ${PROJECT_NAME})
    SET(CPACK_GENERATOR                       "TGZ;RPM;STGZ;")
ENDIF()
SET(CPACK_PACKAGE_NAME                    "${PROJECT_NAME}")
SET(CPACK_PACKAGE_VENDOR                  "Hear And See Systems LLC")
SET(CPACK_PACKAGE_DESCRIPTION             "Cloudy Library")
SET(CPACK_PACKAGE_DESCRIPTION_SUMMARY     "Cmake cloudy - CPack Component Installation Example")
SET(CPACK_PACKAGE_VERSION                 "${PROJECT_MODULE_VERSION_MAJOR}.${PROJECT_MODULE_VERSION_MINOR}.${PROJECT_MODULE_VERSION_PATCH}")
SET(CPACK_PACKAGE_FILE_NAME               "${CPACK_PACKAGE_NAME}-${PROJECT_MODULE_VERSION_MAJOR}.${PROJECT_MODULE_VERSION_MINOR}.${PROJECT_MODULE_VERSION_PATCH}.${CMAKE_SYSTEM_PROCESSOR}")
SET(CPACK_PACKAGE_VERSION_MAJOR           "${PROJECT_MODULE_VERSION_MAJOR}")
SET(CPACK_PACKAGE_VERSION_MINOR           "${PROJECT_MODULE_VERSION_MINOR}")
SET(CPACK_PACKAGE_VERSION_PATCH           "${PROJECT_MODULE_VERSION_PATCH}")
SET(CPACK_PACKAGE_INSTALL_DIRECTORY       "CPack Component Example")
SET(CPACK_PACKAGING_INSTALL_PREFIX        ${CMAKE_INSTALL_PREFIX})
SET(CPACK_PACKAGE_RELOCATABLE             TRUE)
IF(CMAKE_SYSTEM_NAME STREQUAL "Windows")
    SET(CPACK_GENERATOR                   "NSIS")
    SET(CPACK_NSIS_DISPLAY_NAME           "${CPACK_PACKAGE_INSTALL_DIRECTORY} My Famous Project")
ENDIF()

IF(CMAKE_SYSTEM_NAME STREQUAL "Linux")
    # http://www.debian.org/doc/debian-policy/ch-controlfields.html
    # https://wiki.ubuntu.com/PackagingGuide/Basic?action=show&redirect=HowToBuildDebianPackagesFromScratch/
    SET(CPACK_DEBIAN_PACKAGE_NAME             "${PROJECT_NAME}")
    SET(CPACK_DEBIAN_PACKAGE_VERSION          "${CPACK_PACKAGE_VERSION}")
    SET(CPACK_DEBIAN_PACKAGE_MAINTAINER       "Imre Tabur <imre.tabur@eesti.ee>")
    SET(CPACK_DEBIAN_PACKAGE_DESCRIPTION      "Example cmake")
    SET(CPACK_DEBIAN_PACKAGE_PRIORITY         "optional")
    SET(CPACK_DEBIAN_PACKAGE_SECTION          "libs")
    SET(CPACK_DEBIAN_ARCHITECTURE             ${CMAKE_SYSTEM_PROCESSOR})
ENDIF()

include(CPack)